package two.chapter_4.c4_3;

public class Edge implements Comparable<Edge> {
    private int v,w;
    private double weight;
    public Edge(int v,int w,double weight){
        this.v=v;
        this.w=w;
        this.weight=weight;
    }

    public double weight() {
        return weight;
    }

    /**
     * 边两端的顶点之一
     * @return
     */
    public int either(){
        return v;
    }

    /**
     * 另一个顶点
     * @param v
     * @return
     */
    public int other(int v){
        if (v==this.v) return this.w;
        else if (v==this.w)return this.v;
        throw new IllegalArgumentException("参数非法");
    }
    @Override
    public int compareTo(Edge that) {
        if (this.weight()<that.weight()) return -1;
        else if (this.weight()>that.weight()) return 1;
        else return 0;

    }

    @Override
    public String toString() {
        return String.format("%d-%d %.2f",v,w,weight);
    }
}
